﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace DTcms.API.Payment.paypal
{
    /// <summary>
    /// 通知
    /// </summary>
    public class PayPalNotice
    {
       public string NoticeVild(string strRequest) {
            try
            {
               ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                var verificationResponse = string.Empty;
                var verificationRequest = (HttpWebRequest)WebRequest.Create(PaypalConfig.IPNEndpoint);

                //Set values for the verification request
                verificationRequest.Method = "POST";
                verificationRequest.ContentType = "application/x-www-form-urlencoded";


                //Add cmd=_notify-validate to the payload
                strRequest = "cmd=_notify-validate&" + strRequest;
                verificationRequest.ContentLength = strRequest.Length;

                //Attach payload to the verification request
                var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
                streamOut.Write(strRequest);
                streamOut.Close();

                //Send the request to PayPal and get the response
                var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
                verificationResponse = streamIn.ReadToEnd();
                streamIn.Close();
                return verificationResponse;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
